//
//  FMPlayerVC.m
//  NewMyMusicAPP
//
//  Created by FC on 15/9/21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

 //  进入历史收藏页面



#import "FMPlayerVC.h"
#import "FMPersonModel.h"
#import <AVFoundation/AVFoundation.h>

@interface FMPlayerVC ()<STKAudioPlayerDelegate,STKDataSourceDelegate>

@property(nonatomic,retain)NSMutableArray *playerArray;



@end

@implementation FMPlayerVC


//将播放也面设置成单例类
+(instancetype)shareHandleController
{
    //创建单例对象
    static FMPlayerVC *playerVC = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        playerVC = [[FMPlayerVC alloc] init];
        
    });
    
    return playerVC;
}



- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        [self  clickToPlay];
        
    }
    
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.hidesBottomBarWhenPushed = YES;

    
    // 给导航栏上放自定义label
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100 * WIDTH, 30 * HEIGHT)];
    [view setBackgroundColor:[UIColor clearColor]];
    self.navigationItem.titleView = view;
    _myTitle = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH * (-375/2 + 100), 0, WIDTH * 300, HEIGHT * 30)];
    _myTitle.text = self.title;
    _myTitle.textAlignment = NSTextAlignmentCenter;//居中
    _myTitle.textColor = [UIColor whiteColor];
    [_myTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];//加粗字体
    [view addSubview:_myTitle];
    
    
    UIImage *image2 = [UIImage imageNamed:@"VList_Left@2x.png"];
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    
    //根据坐标取出对应的歌曲 链接
    FMPersonModel *model = [_Audio objectAtIndex:self.indexPath.row];
    
    //创建audioStreamer
    self.musicURL = model.playUrl32;
    _url = [NSURL URLWithString:model.playUrl32];
    if (_audioPlayer) {
        _audioPlayer = nil;
        [_FMPlayerV removeFromSuperview];
    }
    _audioPlayer = [[STKAudioPlayer alloc]init];
    [_audioPlayer playURL:_url];//开始播放
    NSLog(@"%@",_url);
    
    _FMPlayerV = [[FMPlayerView alloc]initWithFrame:self.view.bounds array:self.Audio];
    
    [self.view addSubview:_FMPlayerV];
    
    
    
    _FMPlayerV.myID  = self.trackId;
    _FMPlayerV.title = self.title;
    [_FMPlayerV release];
    
    _FMPlayerV.audioPlayer = _audioPlayer;
    
    _FMPlayerV.AudioArray = [NSMutableArray arrayWithArray:self.Audio];
    
    _FMPlayerV.myDelegate = self;
    
    _number = self.indexPath.row ;
    if(_FMPlayerV.isPlaying == YES && _FMPlayerV.isRotating == YES) {
        [_FMPlayerV.streamer  pause];//上一曲暂停
    }

    
    
    
    
    
     [self getMassage];  // 解析数据
//     [self clickToPlay];  // 点击进入播放
    
}


- (void)setPlayUrl:(NSString *)playUrl
{
#pragma mark -- 三方音乐播放器
    NSURL *playUrl_ToString = [NSURL URLWithString:playUrl];
    if (_url != playUrl_ToString) {
        _url = playUrl_ToString;
        [self createPlayer:self.url]; // 创建播放器

    }
    
//    _audioPlayer = [[STKAudioPlayer alloc]init];
//    [_audioPlayer playURL:[NSURL URLWithString:playUrl]];//开始播放
//    NSLog(@"%@",playUrl);
//    
//    _FMPlayerV = [[FMPlayerView alloc]initWithFrame:self.view.bounds array:self.Audio];
//    
//    [self.view addSubview:_FMPlayerV];
//    
//    
//    
//    _FMPlayerV.myID  = self.trackId;
//    _FMPlayerV.title = self.title;
//    [_FMPlayerV release];
//    
//    _FMPlayerV.audioPlayer = _audioPlayer;
//    
//    _FMPlayerV.AudioArray = [NSMutableArray arrayWithArray:self.Audio];
//    
//    _FMPlayerV.myDelegate = self;
//    
//    _number = self.indexPath.row ;
//    if(_FMPlayerV.isPlaying == YES && _FMPlayerV.isRotating == YES) {
//        [_FMPlayerV.streamer  pause];//上一曲暂停
//    }

}


-(void)createPlayer : (NSURL *)playUrl
{
#pragma mark -- 三方音乐播放器
  
    
}


#pragma mark -- 点击播放按钮
-(void)clickToPlay
{
    
    if(_FMPlayerV.indexPath == self.indexPath){
        
        [_FMPlayerV spinOn];//图片旋转
        
        
    }else{
        
        [self getMassage];
        
        FMPersonModel *model = [_Audio objectAtIndex:self.indexPath.row];
        //创建audioStreamer
        
//        NSLog(@"model.url = %@",model.playUrl32);
        
        _url = [NSURL URLWithString:model.playUrl32];
        [_audioPlayer playURL:_url];//开始播放
#pragma mark -- 初始化下一曲 number
        _number = self.indexPath.row ;
        
        _FMPlayerV.indexPath = self.indexPath;
        
        self.musicURL = model.playUrl32;
        [_FMPlayerV ContinueToRotate];//继续旋转
        [_FMPlayerV spinOn];//图片旋转
        [_FMPlayerV.button setBackgroundImage:[UIImage imageNamed:@"toolbar_pause_n@2x.png"] forState:UIControlStateNormal];//播放按钮图片
        _FMPlayerV.isPlaying = YES;
        _FMPlayerV.isRotating = YES;
    }
    
}





#pragma mark -- 播放页面数据解析

-(void)getMassage
{
    
    NSString *string =[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/track/detail?device=iPhone&trackId=%@",self.trackId];
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        NSMutableDictionary *dic = responseObject;
        
       FMPlayerModel *model = [[[FMPlayerModel alloc]init]autorelease];
        
        [model setValuesForKeysWithDictionary:dic];
        
        
        [self.playerArray addObject:model];
        
        self.FMPlayerV.playViewArray = self.playerArray;
        self.FMPlayerV.model = model;

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
    
    
    
}


#pragma mark -- 跳到下一首
-(void)NextMusic
{

    _number ++;

    if (_number >= self.Audio.count ) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您已经是最后一首啦" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
        _number --;
        NSLog(@"最后一首啦");
        return;
    }

    
    if(_FMPlayerV.isPlaying == YES && _FMPlayerV.isRotating == YES) {
        
        [_FMPlayerV.audioPlayer pause];//暂停
    }
    
    FMPersonModel *model = [_Audio objectAtIndex:_number];
    
    //创建audioStreamer
    _url = [NSURL URLWithString:model.playUrl32];
     [_audioPlayer playURL:_url];//开始播放
   _FMPlayerV.audioPlayer = self.audioPlayer;//音频
    _myTitle.text = model.title;
    self.trackId = model.trackId;
    
    
    [self getMassage];
    
    [_FMPlayerV.button setBackgroundImage:[UIImage imageNamed:@"toolbar_pause_n@2x.png"] forState:UIControlStateNormal];//播放按钮图片
    [_FMPlayerV  Recovery];//图片复位
    [_FMPlayerV ContinueToRotate];//继续旋转
    [_FMPlayerV spinOn];

    
    _FMPlayerV.isPlaying = YES;
    _FMPlayerV.isRotating = YES;
    
    

}




#pragma mark -- 跳到上一首
-(void)lastSong
{
    
    if (_number == 0) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您已经是第一首啦" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
        NSLog(@"第一首啦");
        return;
    }
    
    _number --;
    if(_FMPlayerV.isPlaying == YES && _FMPlayerV.isRotating == YES) {
        [_FMPlayerV.audioPlayer pause];//暂停
    }
    
    FMPersonModel *model = [_Audio objectAtIndex:_number];
    //创建audioStreamer
//    NSLog(@"model = %@",model.title);
    _url = [NSURL URLWithString:model.playUrl32];
    [_audioPlayer playURL:_url];//开始播放
    _FMPlayerV.audioPlayer = self.audioPlayer;//音频
    _myTitle.text = model.title;

    self.trackId = model.trackId;
    [self getMassage];
    
    [_FMPlayerV.button setBackgroundImage:[UIImage imageNamed:@"toolbar_pause_n@2x.png"] forState:UIControlStateNormal];//播放按钮图片
    [_FMPlayerV  Recovery];//图片复位
    [_FMPlayerV ContinueToRotate];//继续旋转
    [_FMPlayerV spinOn];
    
    
    
    
    _FMPlayerV.isPlaying = YES;
    _FMPlayerV.isRotating = YES;
    
    
}





-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}



@end
